package com.glsc.ngateway.platform.service.glres;

import com.glsc.ngateway.common.api.common.utils.PageUtil;
import com.glsc.ngateway.common.base.domain.mysql.gateway.platform.glres.GlscSystemAccount;
import com.glsc.ngateway.common.base.repo.mysql.gateway.platform.glres.GlscSystemAccountRepository;
import com.glsc.ngateway.platform.service.UserService;
import com.glsc.ngateway.platform.utils.SpecificationUtil;
import org.springframework.data.domain.*;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import javax.transaction.Transactional;
import java.util.Map;
import java.util.Optional;

@Service
public class GlscSystemAccountService {

    @Resource
    private GlscSystemAccountRepository accountRepository;

    @Resource
    private UserService userService;

    @Resource
    private GlscResourceOperateLogService logService;

    /**
     * 分页查找
     *
     * @param pageNo
     * @param pageSize
     * @param param
     * @return
     */
    public Page<GlscSystemAccount> page(int pageNo, int pageSize, Map<String, Object> param) {
        Sort.Direction direction = Sort.Direction.DESC;
        String sort = "createTime";

        pageNo = PageUtil.initPageNo(pageNo);
        pageSize = PageUtil.initPageSize(pageSize);

        Pageable pageable = PageRequest.of(pageNo - 1, pageSize, direction, sort);
        Page<GlscSystemAccount> page = accountRepository.findAll(SpecificationUtil.buildSpecification(param, GlscSystemAccount.class), pageable);
        return page;
    }


    /**
     * 根据id查找
     *
     * @param id
     * @return
     */
    public GlscSystemAccount findById(Integer id) {
        Optional<GlscSystemAccount> optional = accountRepository.findById(id);
        if (optional.isPresent()) {
            return optional.get();
        }
        return null;
    }

    /**
     * 保存
     *
     * @param obj
     * @return
     */
    @Transactional
    public GlscSystemAccount save(GlscSystemAccount obj) {
        return accountRepository.save(obj);
    }

    /**
     * 根据id删除
     *
     * @param id
     */
    @Transactional
    public void delete(Integer id) {
        accountRepository.deleteById(id);
    }
}
